
var diff = function(a, b) {
	var a_lines = a.split(/\n/);
	var b_lines = b.split(/\n/);
	var index = {}, l;

	for (var i = 0, max = a_lines.length; i < max; ++i) {
		l = 'line: ' + a_lines[i];
		if (!index.hasOwnProperty(l))
			index[l] = [i];
		else
			index[l][index[l].length] = i;
	}
	var pp, pp_area, j, n = -1, n0, ia, ib, io, old_n = 0, out = [];
	
	for (i = 0, max = b_lines.length; i < max; ++i) {
		l = 'line: ' + b_lines[i];
		pp = index[l];
		if (!pp) {
			out[out.length] = b_lines[i];
			n = n0 = -1;
		} else {
			if (n >= 0 && b_lines[i] == a_lines[n + 1]) {
				out[out.length - 1] = '@@ ' + n0 + '..' + (++n);
				old_n = n + 1;
			} else if (pp.length > 1) {
				out[out.length] = b_lines[i];
				n = -1;
				n0 = -1;
			} else {
				n = pp[0];
				n0 = n;
				ia = n - 1;
				ib = i - 1;
				j = 0;
				for (; n0 > old_n && ia >= 0 && out.length > 0 && a_lines[ia] == out[out.length - 1]; ++j, --ia, --ib, --n0)
					out.pop();
				out[out.length] = '@@ ' + n0 + '..' + n;
			}
		}
	}	
	return out.join('\n');
};

